package com.cool.person.controller;

import com.cool.person.entity.User;
import com.cool.person.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author qh
 * @Date 2019-02-25
 * @description
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping("/")
    public String list(Model model) {

        // 获取用户表字段信息
        model.addAttribute("columnsList", userService.listColumns());

        return "index";
    }


    /**
     * 根据前端传过来的字段查询表数据
     * @param ids 要查询的字段
     * @return
     */
    @ResponseBody
    @RequestMapping("/list")
    public Map<String, Object> list2(@RequestParam("ids") String ids) {
        String strings[] = ids.split(",");
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> userList = new ArrayList<>();
        if (!StringUtils.isEmpty(ids)) {
            userList = userService.listUser(ids);
        }
        map.put("userList", userList);
        map.put("colList",strings);
        return map;
    }
}
